package com.api.oa.mapper;


import com.api.oa.model.entity.OaOrg;
import com.api.oa.model.vo.OaOrgTreeVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
@Mapper
public interface OrgMapper {


    @Insert("insert into oa_org (" +
            "orgName," +
            "orgClassify," +
            "orgPid," +
            "orgUserId" +
            ") " +
            "values " +
            "(" +
            "#{business.orgName}," +
            "#{business.orgClassify}," +
            "#{business.orgPid}," +
            "#{business.orgUserId}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "orgId", keyColumn = "id")
    int insert(@Param("business") OaOrg business);


    @Update("update oa_org set orgName = #{org.orgName}, " +
            "orgClassify=#{org.orgClassify}," +
            " orgPid = #{org.orgPid}," +
            " orgUserId  =#{org.orgUserId}  where orgId = #{org.orgId}")
    int update(@Param("org") OaOrg org);


    //给机构添加项目负责人
    @Update("update oa_org set orgUserId = #{orgUserId} where orgId = #{org.orgId}")
    int updateUser(@Param("orgUserId") String orgUserId, @Param("orgId") Long orgId);


    @Delete("delete from oa_org where orgId=#{id}")
    void delete(@Param("id") Long id);


    @Select("<script>select oa_org.*,yq_user.userId as orgUserId,yq_user.uname as orgUserName from oa_org left join yq_user on oa_org.orgUserId = yq_user.userId where <if test='pid == null'> orgPid is null </if> <if test='pid != null'> orgPid = #{pid}</if></script>")
    List<OaOrgTreeVO> list(@Param("pid") Long pid);


    @Select("select * from oa_org where orgId = #{id} limit 1")
    OaOrg findOne(@Param("id") Long id);


    @Select("select * from oa_org where orgUserId = #{id} limit 1")
    OaOrg findOneByUserId(@Param("id") String id);

    //查询近亲节点；
    @Select("<script>select * from oa_org where  (orgPid in (<if test='orgPid != null'>#{orgPid},</if>#{orgId}) <if test='orgPid != null'>  or orgId = #{orgPid}   </if>) " +
            "<if test='to!=null and to!=\"\"'>and orgUserId = #{to}</if>" +
            " </script>")
    OaOrg findNearOne(@Param("orgId") Long id, @Param("orgPid") Long pid, @Param("to") String to);


    @Select("<script>select yq_ins.careType from yq_user left join yq_ins on yq_user.belongInsCode  = yq_ins.insCode where yq_user.userId=#{userId}</script>")
    Long findRelate(@Param("userId") String id);
}
